package com.zephyr.vchcenter.dwrAction;

import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;


import com.zephyr.vchcenter.bean.PbcOriginalData;
import com.zephyr.vchcenter.bean.Pagination;
import com.zephyr.vchcenter.bean.ResultImage;
import com.zephyr.vchcenter.bean.Voucher;
import com.zephyr.vchcenter.bean.VoucherTemp;
import com.zephyr.vchcenter.bpm.action.AuditVerifyAction;
import com.zephyr.vchcenter.dao.IBaseDao;
import com.zephyr.vchcenter.helper.SpringHelper;
import com.zephyr.vchcenter.util.ProperitesHelper;

public class DwrPersistHelper {
	private final Logger log = Logger.getLogger(DwrPersistHelper.class);
	//获取业务对象列表Voucher
	public List<Voucher> getDaoOperate(Voucher voucher){
		System.out.println(voucher.getChkstate());
		Object obj=SpringHelper.getBean("baseDao");
		IBaseDao baseDao=(IBaseDao)obj;
		List list=baseDao.findByExample(voucher);
		return list;
	}
    //根据id获取Voucher
	public Voucher getVoucherById(String id){
		log.debug(id);
		log.debug("getVoucherById");
		Object obj=SpringHelper.getBean("baseDao");
		IBaseDao baseDao=(IBaseDao)obj;
		Voucher voucher=(Voucher)baseDao.findById("Voucher", id);
		log.debug(voucher.getSubno()+"++++++++++");
		log.debug(voucher==null);
		return voucher;
	}
	public List updateDataByDwr(String className, Object inputObj) {
		List list=null;
		try {
			Class objClass=Class.forName(className);
			Object obj=SpringHelper.getBean("baseDao");
			IBaseDao baseDao=(IBaseDao)obj;
			list=baseDao.findByExample(objClass.cast(inputObj));
		} catch (ClassNotFoundException e) {
			log.equals(e);
		} 
		return list;
	}

	/*public Map<String,String> pretreatPbcFile(String state){
		
		
		return null;
	}*/
	
	public void deleteVoucherTemp(List<VoucherTemp> list) {
		Object obj = SpringHelper.getBean("baseDao");
		IBaseDao baseDao = (IBaseDao)obj;
		for(VoucherTemp vouTemp : list) {
			baseDao.delete(vouTemp);
		}
	}
	
	public Pagination getVoucherTempData(Pagination pagination) {
		Object obj = SpringHelper.getBean("baseDao");
		IBaseDao baseDao = (IBaseDao)obj;
		pagination.setHql("from VoucherTemp ");
		return baseDao.paging(pagination);
	}
	
	public Pagination getVoucherData(Voucher voucher, Pagination pagination) {
		Object obj = SpringHelper.getBean("baseDao");
		IBaseDao baseDao = (IBaseDao)obj;
		String idstate = voucher.getIdstate();
		if(idstate!= null && !"".equals(idstate))
			pagination.setHql("from Voucher where idstate='"+idstate+"' ");
		String chkstate = voucher.getChkstate();
		if(chkstate!= null && !"".equals(chkstate))
			pagination.setHql("from Voucher where chkstate='"+chkstate+"' ");
		String idresult = voucher.getIdresult();
		if(idresult!= null && !"".equals(idresult))
			pagination.setHql("from Voucher where idresult='"+idresult+"' ");
		String tallytype = voucher.getTallytype();
		if(tallytype!= null && !"".equals(tallytype))
			pagination.setHql("from Voucher where tallytype='"+tallytype+"' ");
		return baseDao.paging(pagination);
	}
	public String getImageServletUrl() {
		String url = ProperitesHelper.getImageServletUrl();
		log.debug("ImageServlet url: " + url);
		return url;
	}
	
	public static void main(String args[]) {
		DwrPersistHelper dph = new DwrPersistHelper();
//		VoucherTemp vouTmp = new VoucherTemp();
//		vouTmp.setVchsid("0cfdc83bbaba4786bf4ef92a642e4cb2");
//		VoucherTemp vouTmp2 = new VoucherTemp();
//		vouTmp.setVchsid("81b86cf804a046f19418c811d34f55fb");
//		List list = new ArrayList();
//		list.add(vouTmp);
//		list.add(vouTmp2);
//		dph.deleteVoucherTemp(list);
		//List list = dph.getVoucherTempData();
		//System.out.println(((VoucherTemp) list.get(0)).toString());
		
//		Object obj = SpringHelper.getBean("baseDao");
//		IBaseDao baseDao = (IBaseDao)obj;
//		Pagination pagination = new Pagination();
//		pagination.setHql("from Voucher ");
//		pagination.setCurrentPages(1);
//		pagination.setPageSize(20);
//		System.out.println(baseDao.paging(pagination).getResults().toString());
		Object obj=SpringHelper.getBean("baseDao");
		IBaseDao baseDao=(IBaseDao)obj;
		ResultImage resImage=(ResultImage) baseDao.findById("ResultImage", "7f5f4784afec4ed1b1395c674e08cab2");
		Blob idresImg = resImage.getResimg();
		String resImg = null;
		try {
			resImg = new String(idresImg.getBytes(new Long(1), Long.valueOf(idresImg.length()).intValue()));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(resImg);
	}
}
